package com.woniuxy.config;

import com.woniuxy.util.RabbitMQConst;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;


@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue checkOrderStatusQueue() {
        return new Queue(RabbitMQConst.QUEUE_CHECKORDERSTATUS);
    }

    @Bean
    public Queue addSecKillBookQueue() {
        return new Queue(RabbitMQConst.QUEUE_ADDSECKILLBOOK);
    }

    @Bean
    public CustomExchange customExchange() {
        /**
         * 参数1:交换机名字
         * 参数2:交换机类型(指定为延迟交换机)
         * 参数3:是否持久化
         * 参数4:是否自动删除
         * 参数5:其他参数
         */
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-delayed-type", "topic");
        return new CustomExchange(RabbitMQConst.EXCHANGE_CUSTOM, "x-delayed-message", true, false, map);
    }
    @Bean
    public Binding checkOrderQueueToCustomExchange(Queue checkOrderStatusQueue, CustomExchange customExchange) {
        return BindingBuilder.bind(checkOrderStatusQueue).to(customExchange).with(RabbitMQConst.ROUTERKEY_CHECKORDERSTATUS).noargs();
    }

    @Bean
    public Binding addSecKillBookQueueToCustomExchange(Queue addSecKillBookQueue, CustomExchange customExchange) {
        return BindingBuilder.bind(addSecKillBookQueue).to(customExchange).with(RabbitMQConst.ROUTERKEY_ADDSECKILLBOOK).noargs();
    }
    /**
     * 秒杀订单队列
     */
    @Bean
    public Queue addKillOrderQueue() {
        return new Queue(RabbitMQConst.QUEUE_ADDKILLORDER);
    }
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(RabbitMQConst.EXCHANGE_TOPIC);
    }

    @Bean
    public Binding addKillOrderQueueToTopicExchange(Queue addKillOrderQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(addKillOrderQueue).to(topicExchange).with(RabbitMQConst.ROUTERKEY_ADDKILLORDER);
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

}
